package com.tuanbiao.presenter.setting;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.hzjq.entity.DeviceInfoEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.SettingController;

/**
 * @author abiao
 * @since 2022/5/11 17:53
 */
public class SettingDeviceNumPresenter extends BasePresenter<SettingController.Model, SettingController.DeviceNumView> implements SettingController.DeviceNumPresenter {
    private DeviceInfoEntity mDeviceInfoEntity;

    public SettingDeviceNumPresenter(SettingController.Model model, SettingController.DeviceNumView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        mModel.getDeviceInfo(entity -> {
            mDeviceInfoEntity = entity;
            mRootView.showDeviceNum(entity.getNum(), mModel.getSettingParams().getEnableLocalDeviceNum());
        });
    }


    @Override
    public void changeNum(boolean use, String content) {
        if (TextUtils.isEmpty(content)) {
            mRootView.showMessage("请输入设备编号");
            return;
        }
        if (null != mDeviceInfoEntity) {
            mDeviceInfoEntity.setNum(content);
            mModel.updateDeviceInfo(use, mDeviceInfoEntity, unused -> {
                mRootView.showDeviceNum(content, use);
//                getDeviceInfo();
                mRootView.showMessage("修改成功");
            });
        }
    }

    @Override
    public void reset() {
        if (null != mDeviceInfoEntity) {
            mRootView.showDeviceNum(mDeviceInfoEntity.getOriginalNum(), mModel.getSettingParams().getEnableLocalDeviceNum());
        } else {
            mRootView.showMessage("初始化失败");
        }
    }
}